// 自定义 Promise 构造函数

class Promise {
  // 构造方法
  constructor(executor) {
    // 创建 Promise 构造函数 - 参数：执行函数
    // *添加属性
    this.PromiseState = 'pending';
    this.PromiseResulve = null;
    this.callBack = [];  // 保存回调的对象

    // 保存实例对象的 this
    const that = this;

    // *声明函数(resolve,reject)
    function resolve(data) {
      // 判断状态
      if (that.PromiseState !== 'pending') return;
      //修改状态
      that.PromiseState = 'resolved';
      // 设置对象结果
      that.PromiseResulve = data;

      // 执行异步回调
      setTimeout(() => {
        that.callBack.forEach(item => {
          item.onResolved(data)
        })
      })
    }
    function reject(data) {
      // 判断状态
      if (that.PromiseState !== 'pending') return
      //修改状态
      that.PromiseState = 'rejected'
      // 设置对象结果
      that.PromiseResulve = data

      // 执行异步回调
      setTimeout(() => {
        that.callBack.forEach(item => {
          item.onRejected(data)
        })
      })
    }

    try {
      // 同步调用执行器函数
      executor(resolve, reject)
    } catch (error) {
      // 改变 Promise 状态
      reject(error)
    }
  }

  // then 方法
  then(onResolved, onRejected) {
    const that = this
    // 异常穿透
    if (typeof onRejected !== 'function') {
      onRejected = reason => {
        throw reason
      }
    }
    // 值传递
    if (typeof onResolved !== 'function') {
      onResolved = valur => valur
    }

    // 执行回调
    return new Promise((resolve, reject) => {
      // 封装函数
      function callBack(type) {
        try {
          const results = type(that.PromiseResulve);
          // 判断结果类型
          if (results instanceof Promise) {
            // Promise 类型
            results.then((value) => {
              resolve(value);
            }, (reason) => {
              reject(reason)
            })
          } else {  // 非 Promise 类型
            resolve(results);
          }
        } catch (error) {
          reject(error)
        }
      }

      //成功
      if (this.PromiseState === 'resolved') {
        setTimeout(() => {
          callBack(onResolved)
        })
      }
      // 失败
      if (this.PromiseState === 'rejected') {
        setTimeout(() => {
          callBack(onRejected)
        })
      }

      // pending
      if (this.PromiseState === 'pending') {
        // 保存函数
        this.callBack.push({
          onResolved: function () {
            callBack(onResolved)
          },
          onRejected: function () {
            callBack(onRejected)
          }
        })
      }
    })
  }

  catch(onRejected) {
    return this.then(undefined, onRejected)
  }

  // Promise.resolve()方法
  // static 表示此方法是静态成员，属于类，不属于成员方法
  static resolve(value) {
    return new Promise((resolve, reject) => {
      if (value instanceof Promise) {
        // Promise对象-判断状态
        value.then((value) => {
          resolve(value)
        }, (reason) => {
          reject(reason)
        })
      } else {
        // 非 Promise 对象-状态成功
        resolve(value)
      }
    })
  }

  // Promise.reject()方法
  static reject(reason) {
    return new Promise((resolve, reject) => {
      reject(reason)
    })
  }

  // Promise.all()方法
  static all(promise) {
    return new Promise((resolve, reject) => {
      let count = 0;
      const arr = [];
      promise.forEach((item, index) => {
        item.then((value) => {
          count++;
          arr[index] = value
          if (count === promise.length) {
            resolve(arr);
          }
        }, (reason) => {
          reject(reason);
        })
      })
    })
  }
  // Promise.race()方法
  static race(promise) {
    return new Promise((resolve, reject) => {
      promise.forEach((item, index) => {
        item.then((v) => {
          resolve(v)
        }, (r) => {
          reject(r)
        })
      })
    })
  }
}
